<script setup lang="ts">
import {
  propertyDelApi,
  propertyGetBuildListApi,
  propertyGetEnListApi,
  propertyGetList,
} from '@/apis/property'
import type { ListDataSimpleI, ListParams } from '@/types'
import type { PropertyI } from '@/types/property.d.ts'
import { ref } from 'vue'
import BillDialog from './components/BillDialog.vue'
import { ElMessage } from 'element-plus'

const listParams = ref<ListParams>({
  page: 1,
  pageSize: 8,
  enterpriseName: '',
  start: '',
  end: '',
})
const timeList = ref<string[]>([])
const list = ref<PropertyI[]>([])
const total = ref(0)
let enList: ListDataSimpleI[] = []
let buildingList: ListDataSimpleI[] = []
const getList = async () => {
  if (timeList.value) {
    listParams.value.start = timeList.value[0]
    listParams.value.end = timeList.value[1]
  } else {
    listParams.value.start = ''
    listParams.value.end = ''
  }

  const res = await propertyGetList(listParams.value)
  list.value = res.rows
  total.value = res.total
  enList = await propertyGetEnListApi()
  buildingList = await propertyGetBuildListApi()
}
getList()
const onSearch = () => {
  listParams.value.page = 1
  listParams.value.pageSize = 8
  getList()
}
const changePage = (page: number) => {
  listParams.value.page = page
  getList()
}
const changePageSize = (pageSize: number) => {
  listParams.value.page = 1
  listParams.value.pageSize = pageSize
  getList()
}
const onDel = async (id: number) => {
  await propertyDelApi(id)
  getList()
  ElMessage.success('删除成功~')
}
/**
 * 弹层
 */
const dialogVisible = ref(false)
const billDetail = ref<PropertyI>()
const billDialogVisible = ref(false)
const viewBill = (row: PropertyI) => {
  billDetail.value = row
  billDialogVisible.value = true
}
</script>

<template>
  <div class="page-container">
    <!-- 搜索区域 -->
    <el-form :inline="true" class="header-container">
      <el-form-item label="企业名称:">
        <el-input
          placeholder="请输入企业名称"
          v-model="listParams.enterpriseName"
        />
      </el-form-item>
      <el-form-item label="缴费时间:">
        <el-date-picker
          v-model="timeList"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮区域 -->
    <el-button type="primary" @click="dialogVisible = true">添加账单</el-button>
    <!-- 表格区域 -->
    <el-table :data="list">
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column label="账单编号" align="center" prop="billNumber" />
      <el-table-column label="企业名称" align="center" prop="enterpriseName" />
      <el-table-column label="租赁楼宇" align="center" prop="buildingName" />
      <el-table-column
        label="物业费(m²)"
        align="center"
        prop="propertyFeePrice"
      />
      <el-table-column
        label="账单金额(元)"
        align="center"
        prop="paymentAmount"
      />
      <el-table-column label="缴费时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="viewBill(row)">查看</el-button>
          <el-popconfirm title="确定删除吗?" @confirm="onDel(row.id)">
            <template #reference>
              <el-button type="text">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="listParams.page"
      v-model:page-size="listParams.pageSize"
      layout="total, prev, pager, next, slot, jumper"
      :total="total"
      @current-change="changePage"
    >
      <el-select
        style="width: 95px"
        v-model="listParams.pageSize"
        @change="changePageSize"
      >
        <el-option label="8条/页" :value="8" />
        <el-option label="10条/页" :value="10" />
        <el-option label="15条/页" :value="15" />
      </el-select>
    </el-pagination>
    <!-- 添加弹层区域 -->
    <bill-dialog
      v-model:dialogVisible="dialogVisible"
      :enList="enList"
      :buildingList="buildingList"
      @success="getList"
    />
    <!-- 查看弹层区域 -->
    <el-dialog
      title="查看账单"
      width="35%"
      top="10vh"
      v-model="billDialogVisible"
    >
      <el-form style="width: 55%; margin: 10px auto" :model="billDetail">
        <el-form-item label="租户名称:">
          <span>{{ billDetail?.enterpriseName }}</span>
        </el-form-item>
        <el-form-item label="租赁楼宇:">
          <span>{{ billDetail?.buildingName }}</span>
        </el-form-item>
        <el-form-item label="缴费周期:">
          <span>{{ billDetail?.startTime + '至' + billDetail?.endTime }}</span>
        </el-form-item>
        <el-form-item label="物业费(元/m²):">
          <span>{{ billDetail?.propertyFeePrice }}</span>
        </el-form-item>
        <el-form-item label="账单金额(元):">
          <span>{{ billDetail?.paymentAmount }}</span>
        </el-form-item>
        <el-form-item label="支付方式:">
          <span>{{ billDetail?.paymentMethod }}</span>
        </el-form-item>
        <el-form-item label="缴费时间:">
          <span>{{ billDetail?.createTime }}</span>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="billDialogVisible = false"
          >确定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.page-container {
  padding: 20px;

  .header-container {
    .el-form {
      margin-bottom: 20px;
      border-bottom: 1px solid rgba(0, 0, 0, 0.1);

      ::v-deep(.el-form-item__label) {
        color: rgb(39, 39, 39);
        font-weight: 500;
      }
    }
  }

  .el-table {
    width: 100%;
    height: 400px;
    margin: 20px 0;

    ::v-deep(.el-table__header th) {
      background-color: rgb(237, 237, 237, 0.9);
      color: rgb(43, 37, 37);
      padding: 10px 0;
    }

    ::v-deep(.el-table__body tr) {
      height: 50px;
    }
  }

  .el-pagination {
    display: flex;
    justify-content: flex-end;
  }

  .el-form-item span {
    color: black;
  }
}
</style>
